/*
 * Copyright (c) 2008, Xiong Yingfei, the University of Tokyo All rights
 * reserved.
 */
package ac.jp.u_tokyo.SyncLib.tests;

import static org.junit.Assert.assertArrayEquals;

import java.util.HashMap;

import org.antlr.runtime.RecognitionException;
import org.junit.Test;

import ac.jp.u_tokyo.SyncLib.Mod;
import ac.jp.u_tokyo.SyncLib.NullMod;
import ac.jp.u_tokyo.SyncLib.SwitchCombinator;
import ac.jp.u_tokyo.SyncLib.Sync;
import ac.jp.u_tokyo.SyncLib.SynchronizationFailedException;
import ac.jp.u_tokyo.SyncLib.dictionaries.StaticGet;
import ac.jp.u_tokyo.SyncLib.modParser.ModParser;

public class SwitchCombinatorTest extends SyncTestBase
{

	@Test
	public void testSameBranch() throws SynchronizationFailedException,
			RecognitionException
	{
		SwitchCombinator s = create();
		checkedSynchronize(s, ModParser.parse("{1->2, 2->3}, 2"));
		assertArrayEquals(ModParser.parse("{1->3}, 3"), s
				.synchronize(ModParser.parse("{1->3}, nomod")));

	}

	@Test
	public void testOtherBranch() throws SynchronizationFailedException, RecognitionException
	{
		SwitchCombinator s = create();
		checkedSynchronize(s, ModParser.parse("{1->2, 2->3}, 2"));
		assertArrayEquals(ModParser.parse("{1->3, 2->5}, 5"), s
				.synchronize(ModParser.parse("{1->3}, 5")));
	}

	private SwitchCombinator create() throws SynchronizationFailedException
	{
		Sync[] syncs = new Sync[2];
		syncs[0] = new StaticGet<Integer>(1, true);
		syncs[1] = new StaticGet<Integer>(2, true);
		ac.jp.u_tokyo.SyncLib.SwitchCombinator result = new ac.jp.u_tokyo.SyncLib.SwitchCombinator(syncs);
		result.resynchronize(new Object[] {new HashMap(), null}, new Mod[] {NullMod.INSTANCE, NullMod.INSTANCE});
		return result;

	}
}
